/*
  Author: Ionel Crisu - crisuionel@yahoo.com
  support@FlashExtended.com
*/
package com.flashextended.utils.graphics.windows
{
	import flash.events.MouseEvent;
	
	import mx.containers.Canvas;
	import mx.containers.HBox;
	import mx.containers.Panel;
	import mx.controls.Button;
	import mx.controls.Spacer;
	import mx.core.UIComponent;
	import mx.effects.Fade;
	import mx.effects.Move;
	import mx.effects.Resize;
	import mx.events.EffectEvent;
	import mx.managers.PopUpManager;

	public class GenericInfoWindow extends Panel
	{
		private static const EFF_DURATION:Number=300;
		private var okText:String = "oK";
		private var cancelText:String = "Cancel";
		
		private var okBTN:Button;
		private var cancelBTN:Button;
		private var content:UIComponent;
		
		private var wdt:Number;
		private var hgt:Number;
		
		private var footer:HBox;
		
		public function GenericInfoWindow(w:Number, h:Number, title:String="Info")
		{
			super();
			this.wdt = w;
			this.hgt = h;
			this.width = w;
			this.height = h;
			this.title = title;
			this.setStyle("borderAlpha", .4);
			this.setStyle("borderColor", 0xd2e0f7);
			this.visible = false;
			construct();
		}
		
		private function construct():void
		{
			content = new Canvas();
			content.percentWidth = 100;
			content.percentHeight = 100;
			
			footer = new HBox();
			footer.percentWidth = 100;
			footer.height = 40;
			footer.setStyle("backgroundColor", 0xedf3fc);
			footer.setStyle("paddingTop", 8);
			
			footer.setStyle("paddingRight", 10);
			
			var sp:Spacer = new Spacer();
			sp.percentWidth = 100;
			footer.addChild(sp);
			
			okBTN = new Button();
			okBTN.label = okText;
			footer.addChild(okBTN);
			
			cancelBTN = new Button();
			cancelBTN.label = cancelText;
			footer.addChild(cancelBTN);
			
			okBTN.addEventListener(MouseEvent.CLICK, okMouseHandler);
			cancelBTN.addEventListener(MouseEvent.CLICK, cancelMouseHandler);
			
			this.addChild(content);
			this.addChild(footer);
		}
		
		public function show(isEffect:Boolean=true):void
		{
			if(!isEffect)
			{
				this.visible = true;
			}
			else
			{
				this.width = 50;
				this.height = 50;
				this.visible = true;
				
				var rs:Resize = new Resize(this);
				rs.widthFrom = this.width;
				rs.heightFrom = this.height;
				rs.widthTo = wdt;
				rs.heightTo = hgt;
				rs.duration = EFF_DURATION;
				
				var px:Number = this.x;
				var py:Number = this.y;
				this.x += wdt/2-25;
				this.y += hgt/2-25;
				var mv:Move = new Move(this);
				mv.xFrom = this.x;
				mv.yFrom = this.y;
				mv.xTo = px;
				mv.yTo = py;
				mv.duration = EFF_DURATION;
				
				rs.end();
				rs.play();				
				mv.end();
				mv.play();
			}
			
		}
		
		public function getContentPane():UIComponent
		{
			return content;
		}
		
		/**
		 * TB overrided
		 */ 
		public function okMouseHandler(e:MouseEvent):void{}
		/**
		 * TB overrided
		 */ 		
		public function cancelMouseHandler(e:MouseEvent=null):void
		{
			var rs:Resize = new Resize(this);
			rs.addEventListener(EffectEvent.EFFECT_END, closeWindow);
			rs.widthFrom = wdt;
			rs.heightFrom = hgt;
			rs.widthTo = 50;
			rs.heightTo = 50;
			rs.duration = EFF_DURATION;
			
			var fade:Fade = new Fade(this);
			fade.alphaFrom = 1;
			fade.alphaTo = 0;
			fade.duration = EFF_DURATION;
			
			var px:Number = this.x+wdt/2;
			var py:Number = this.y+hgt/2;
			var mv:Move = new Move(this);
			mv.xFrom = this.x;
			mv.yFrom = this.y;
			mv.xTo = px;
			mv.yTo = py;
			mv.duration = EFF_DURATION;			
			
			rs.end();
			rs.play();			
			fade.end();
			fade.play();
			mv.end();
			mv.play();	
		}
		
		public function closeWindow(e:EffectEvent=null):void
		{
			PopUpManager.removePopUp(this);
		}
		
		public function getOkButton():Button
		{
			return okBTN;
		}
		public function getCancelButton():Button
		{
			return cancelBTN;
		}
		
		public function getFooter():HBox
		{
			return footer;
		}
		
		public function set w(val:Number):void
		{
			wdt = val;
		}
		public function set h(val:Number):void
		{
			hgt = val;
		}		
		
		
	}
}